package com.lixiang.userinfo;

import static org.junit.Assert.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lixiang.userinfo.dao.UserMapper;
import com.lixiang.userinfo.entity.User;
/**
 * 客户信息测试类
 * @author 李湘
 * @date 2018年10月22日
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UserTest {
	
	//访问链接
	//public static final String POST_URL = "/user/delete/1";//删除
	//public static final String POST_URL = "/user/save?userName=真真&userPhone=13485792265&userSex=2&userLogcount=1&saleId=2";//添加
	//public static final String POST_URL = "/user/update?userName=真真&userPhone=13485792265&userSex=2&userLogcount=1&saleId=2&userState=1&userId=5";//更新
	//public static final String POST_URL = "/user/selectWeek/1";//按周查询
	//public static final String POST_URL = "/user/selectMonth/1";//按月查询
	//public static final String POST_URL = "/user/selectYear/1";//按年查询
	//public static final String POST_URL = "/user/selectLogtime/1/2018-10-22";//指定日期查询
	public static final String POST_URL = "/user/selectAll/1";//查询
	
	
	@Autowired //测试框架
	private MockMvc mockMvc; 
	
	@Autowired 
	private ObjectMapper objectMapper; 
	
	@Autowired 
	private UserMapper postRepository; //当前测试方法需要使用到的dao层mapper
	
	@Test //测试用例
	public void testCreatePost_returnSuccess() throws Exception { 
		ResultActions resultActions = sendCreatePostRequest(); 
		checkCreateValidPostResult(resultActions); 
		
	} 
	//发送请求 返回ResultAction对象 
	private ResultActions sendCreatePostRequest() throws Exception { 
		User postCreateDTO = new User();
		return mockMvc.perform(post(POST_URL) 
				.contentType(MediaType.APPLICATION_JSON) 
				.content(objectMapper.writeValueAsString(postCreateDTO))); 
	} 
	
	
	private void checkCreateValidPostResult(ResultActions resultActions) throws Exception { 
		resultActions.andExpect(status().isCreated());
		//User是当前测试方法所需要用到的entity包下的实体类
		User postFromRsp = transferResponse2PostEntity(resultActions); 
		int postFromDB = postRepository.delete(postFromRsp.getUserId()); 
		assertNotNull(postFromDB); 
	} 
	private User transferResponse2PostEntity(ResultActions resultActions) throws java.io.IOException { 
		String response = resultActions.andReturn().getResponse().getContentAsString();
											//当前实体类.class 
		return objectMapper.readValue(response, User.class); 
	} 

}
